<CFQUERY name="qryEFORMSWITHDOCNUM" datasource="#CLIENT.GLOBAL_DSN#">
	SELECT TABLENAME,count(tablename) as thecount
	FROM GRGBOSETABLEFIELDS
	WHERE COLUMNNAME = 'DOCNUMBER'
		or COLUMNNAME = 'ACTIONBY'
		or COLUMNNAME = 'DATEACTIONWASDONE'	
	group by tablename
</CFQUERY>
<CFQUERY name="qryEFORMSWITHACTIONBY" dbtype="query">
	SELECT TABLENAME
	FROM qryEFORMSWITHDOCNUM
		where thecount = 3
</CFQUERY>
<CFQUERY name="qryECRGFORMS" datasource="#CLIENT.COMPANY_DSN#">
	SELECT FORMCODE, DESCRIPTION,FORMGROUP,TABLENAME_FK
	FROM ECRGFORMS
	WHERE TABLENAME_FK <> 'ECINDTRUPDATE'
		AND TABLENAME_FK IN (#QUOTEDVALUELIST(qryEFORMSWITHACTIONBY.TABLENAME)#)
	ORDER BY DESCRIPTION
</CFQUERY>
<cfif structkeyexists(form,"hidedtrcode")>
	<cfset autogenvalue = form.hidedtrcode>
	<cfquery name="qryecrgdtr" datasource="#client.company_dsn#">
		select recdatecreated, reccreatedby
		from ecrgdtr
		where edtrcode = '#autogenvalue#'
	</cfquery>
	<cfquery name="qryecrgdtrdtl" datasource="#client.company_dsn#">
		select recdatecreated, reccreatedby
		from ecrgdtrdtl
		where edtrcode = '#autogenvalue#'
	</cfquery>
	<cfquery name="delecrgdtr" datasource="#client.company_dsn#">
		delete from ecrgdtr
		where edtrcode = '#autogenvalue#'
	</cfquery>
	<cfquery name="delecrgdtrdtl" datasource="#client.company_dsn#">
		delete from ecrgdtrdtl
		where edtrcode = '#autogenvalue#'
	</cfquery>

    <cfset cuttoff = edtCutoffdate &' '&TimeFormat(edtCutofftime,"HH:mm")>
    <cfif not Len(qryecrgdtr.recdatecreated)>
		<cfset recdatecreated = CreateODBCDate(Now())>
    <cfelse>
    	<cfset recdatecreated = CreateODBCDate(qryecrgdtr.recdatecreated)>  	
    </cfif>
	<CFQUERY name="insertECRGDTR" datasource="#CLIENT.COMPANY_DSN#">
		INSERT INTO ECRGDTR 
        	(EDTRCODE,EDTRGROUP,EDTRDESC,TKFILENAME_FK,SHAREDVIEW,SHAREDUPDATE,
        		VIEWUNTIL,UPDATEUNTIL,CUTOFF,GROUPCODE,USERID,TIMELASTUPDATE,
        		DATELASTUPDATE,RECCREATEDBY,RECDATECREATED)
			
           VALUES(
               '#AUTOGENVALUE#',
               '#FORM.edtEDTRGROUP#',
               '#FORM.edtEDTRDESC#',
               '#FORM.edtTKFILENAME_FK#',
               '#FORM.EDTSHAREDVIEW#',
               '#FORM.EDTSHAREDUPDATE#',
               #CreateODBCDate(FORM.EDTVIEWUNTIL)#,
               #CreateODBCDate(FORM.EDTUPDATEUNTIL)#,
               #CreateODBCDateTime(cuttoff)#,
               '#FORM.EDTGROUPCODE#',
               '#CLIENT.USERID#',
               '#TIMEFORMAT(NOW(),"HH:MM")#',
               #CreateODBCDate(NOW())#,
               '#qryecrgdtr.reccreatedby#',
               #recdatecreated#
           )
	</CFQUERY>
    
	<cfif not Len(qryecrgdtrdtl.recdatecreated)>
		<cfset recdatecreated = CreateODBCDate(Now())>
    <cfelse>
    	<cfset recdatecreated = CreateODBCDate(qryecrgdtrdtl.recdatecreated)>  	
    </cfif>
    
	<CFQUERY NAME="insertECRGDTRDTL" datasource="#CLIENT.COMPANY_DSN#">
		INSERT INTO ECRGDTRDTL
        (EDTRCODE,FORMCODE_FK,DATEFIELD,RECCREATEDBY,RECDATECREATED,USERID,TIMELASTUPDATE,DATELASTUPDATE)
		<CFLOOP list="#FORM.chkEFORMS#" index="EFORM">
		<CFSET THEFC = EFORM>
			SELECT '#AUTOGENVALUE#',
            '#EFORM#',
			<CFIF THEFC EQ "PX">''</CFIF>
            	<CFLOOP query="qryECRGFORMS">
					<CFIF THEFC EQ FORMCODE>
                        <CFIF STRUCTKEYEXISTS (FORM,"selDATE#CURRENTROW#")>
                            '#EVALUATE("form.selDATE#CURRENTROW#")#'
                         <CFELSE>''
                         </CFIF>
                     </CFIF>
                 </CFLOOP>,
                 '#qryecrgdtrdtl.reccreatedby#',
                 #recdatecreated#,
                 '#client.userid#',
                 '#timeformat(now(),"HH:MM")#',
                 #CreateODBCDate(now())#
				 <CFIF EFORM NEQ LISTLAST(FORM.CHKEFORMS)>UNION ALL</CFIF>
		</CFLOOP>
	</CFQUERY>
<cfelse>
	<CFSET AUTOGENVALUE = CREATEUUID()>
    <cfset cuttoff = edtCutoffdate &' '&TimeFormat(edtCutofftime,"HH:mm")>
		<CFQUERY name="insertECRGDTR" datasource="#CLIENT.COMPANY_DSN#">
		INSERT INTO ECRGDTR 
        	(EDTRCODE,EDTRGROUP,EDTRDESC,TKFILENAME_FK,SHAREDVIEW,SHAREDUPDATE,
        		VIEWUNTIL,UPDATEUNTIL,GROUPCODE,RECCREATEDBY,RECDATECREATED, CUTOFF)
			VALUES(
              '#AUTOGENVALUE#',
              '#FORM.edtEDTRGROUP#',
              '#FORM.edtEDTRDESC#',
              '#FORM.edtTKFILENAME_FK#',
              '#FORM.EDTSHAREDVIEW#',
              '#FORM.EDTSHAREDUPDATE#',
              #CreateODBCDate(FORM.EDTVIEWUNTIL)#,
              #CreateODBCDate(FORM.EDTUPDATEUNTIL)#,
              '#FORM.EDTGROUPCODE#',
              '#CLIENT.USERID#',
              #CreateODBCDate(NOW())#,
              #CreateODBCDateTime(cuttoff)#
           )
	</CFQUERY>
	<CFQUERY NAME="insertECRGDTRDTL" datasource="#CLIENT.COMPANY_DSN#">
		INSERT INTO ECRGDTRDTL(EDTRCODE,FORMCODE_FK,DATEFIELD,RECCREATEDBY,
        		RECDATECREATED,USERID,TIMELASTUPDATE,DATELASTUPDATE)
		<CFLOOP list="#FORM.chkEFORMS#" index="EFORM">
		<CFSET THEFC = EFORM>
			SELECT '#AUTOGENVALUE#',
            '#EFORM#',
			<CFIF THEFC EQ "PX"> ''</CFIF>
            <CFLOOP query="qryECRGFORMS">
				<CFIF THEFC EQ FORMCODE>
					<CFIF STRUCTKEYEXISTS (FORM,"selDATE#CURRENTROW#")>
                    	'#EVALUATE("form.selDATE#CURRENTROW#")#'
                    <CFELSE>
                    	''
                    </CFIF>
                </CFIF>
            </CFLOOP>,
            '#CLIENT.USERID#',
            #CreateODBCDate(NOW())#,
            '#client.userid#',
            '#timeformat(now(),"HH:MM")#',
            #CreateODBCDate(now())#
			<CFIF EFORM NEQ LISTLAST(FORM.CHKEFORMS)>UNION ALL</CFIF>
		</CFLOOP>
	</CFQUERY>
</cfif>

